import scrapy
from scrapy.selector import Selector
from ..items import TianyanItem


class TianyanspiderSpider(scrapy.Spider):
    name = 'tianyanspider'
    #allowed_domains = ['https://v.tlyy.cc/movie.html']
    start_urls = ['https://v.tlyy.cc/dy/dy1/index.html']  #爬取动作首页
    ua='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'

    def parse(self, response):
     #  item = TianyanItem()    #创建item对象
        selector = Selector(response) #使用Selector 对请求返回的响应网页内容进行解析
        links_t = selector.xpath('/html/body/div[4]/ul/li[1]/a/@href').extract() #点击详细页链接
        links_r = 'https://v.tlyy.cc/'
        for links in links_t:
            link = links_r+links
            yield scrapy.Request(link,callback=self.parse_details,headers={'User-Agent':self.ua})

            #爬取下一个页面

        next = response.xpath('//div[@class="page"]//a[@class="next"]/@href').extract_first()  #下一个页面链接
        urls = 'https://v.tlyy.cc/'
        if next:
            yield scrapy.Request(url=urls+next,callback=self.parse,headers={'User-Agent':self.ua})
          # #  url_tests=['https://v.tlyy.cc/dy/dy1/index{}.html'.format(str(i)) for i in range(2,4)]
          #  # for url_test in url_tests:
          #  #     yield scrapy.Request(url=url_test,callback=self.parse,headers={'User-Agent':self.ua})
        else :
            url_nexts = ['https://v.tlyy.cc/dy/dy{}/index182.html'.format(str(i)) for i in range(5,9)]
            for url_next in url_nexts:
                yield scrapy.Request(url=url_next, callback=self.parse, headers={'User-Agent': self.ua})

    def parse_details(self,reponse):
        name = reponse.xpath('/html/body/div[4]/div/h1/text()').extract_first()  # 电影名称
        actor = reponse.xpath('/html/body/div[4]/div/div[1]/a/text()').extract()[0]  # 主演
        type = reponse.xpath('/html/body/div[4]/div/div[2]/text()').extract_first()  # 类型
        state = reponse.xpath('/html/body/div[4]/div/div[3]/text()').extract_first()  # 状态
        year = reponse.xpath('/html/body/div[4]/div/div[4]/p[1]/text()').extract_first()  # 年份
        new_time = reponse.xpath('/html/body/div[4]/div/div[4]/p[2]/text()').extract_first()  # 更新时间
        language = reponse.xpath('/html/body/div[4]/div/div[5]/text()').extract_first()  # 语言
        intro = reponse.xpath('//div[@class="description"]/text()').extract_first()  # 介绍
        item = TianyanItem()
        item['name'] = name
        item['actor'] = actor
        item['director'] = None
        item['type'] = type
        item['state'] = state
        item['year'] = year
        item['new_time'] = new_time
        item['language'] = language
        item['area']  = None
        item['play'] = None
        item['intro'] = intro
        yield item






